package cn.sx.service;

/*
 *@program:sx_emall_ssm
 *@author: sx
 *@desc:前台用户登录
 *@Time: 2020/9/18  17:49
 */

import cn.sx.dao.UserDao;
import cn.sx.entity.User;
import cn.sx.util.SafeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService  {

    @Autowired
    private UserDao userDao;

    /*查询全部*/
    public List<User> findAllUser(){
        return userDao.queryAllRecords();
    }

    /*总数 */
    public long getCount(){
        return userDao.selectCount();
    }

    /*根据id查询 */
    public User get (int id){
        return userDao.select(id);
    }

    /*注册*/
    public boolean add(User user) {
        /*密码加密*/
        user.setPassword(SafeUtil.encode(user.getPassword()));
        return userDao.insert(user);
    }

    /*根据用户名和密码查询（登录）*/
    public User getByUsernameAndPassword(String username, String password){
        return userDao.selectByUsernameAndPassword(username,SafeUtil.encode(password));
    }

    /*通过用户名获取（验证） */
    public User getByUsername(String username) {
       return userDao.selectByUsername(username);
    }

    /*修改个人信息收货地址*/
    public boolean updateAddress(int id,String name,String phone,String address){
        User user = new User();
        user.setId(id);
        user.setName(name);
        user.setPhone(phone);
        user.setAddress(address);
        return userDao.updateAddress(user);
    }
    /*修改用户密码*/
    public boolean updateUsersPassword(int id,String password){
        System.out.println("password=" + password);
        return userDao.updatePassword(id,SafeUtil.encode(password));
    }
    /*删除用户*/
    public int deleteUser(int id){
        return userDao.deleteUser(id);
    }

}
